package server.core.dao;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import server.core.base.Airplane;
import server.core.base.Airport;
import server.core.base.Flight;
import server.core.base.Terrain;
import server.core.base.Ticket;
import server.core.base.Ticket.TicketClass;
import server.core.base.User;
import server.core.dao.DaoFactory.daoType;
import server.core.util.FormatDate;

public abstract class DaoLoad {
	protected static final Logger LOCAL_LOG=LogManager.getLogger(LogManager.ROOT_LOGGER_NAME);
	
	public static void loadDao(daoType type){
		if(daoType.FILE.equals(type)){
			String pathname="./cfg.bin";
			File cfg = new File(pathname);
			if(cfg.exists())
				readDao();
			else{
				defaultDao();
				writeDao();
				readDao();
			}
		}else
			defaultDao();
		LOCAL_LOG.info("Start using DAO - "+type);
	}
	
	private static void defaultDao(){
		
		Airplane boeing747 = new Airplane("Boeing 747", 525);
		Airplane airbusA380 = new Airplane("Airbus A380", 853);
		Airplane boeing787 = new Airplane("boeing787", 0);
		Airplane e190 = new Airplane("Embraer E-Jet E190", 0);
		DaoFactory.getDao(daoType.MEMORY).saveObject(boeing747);
		DaoFactory.getDao(daoType.MEMORY).saveObject(airbusA380);
		DaoFactory.getDao(daoType.MEMORY).saveObject(e190);
		DaoFactory.getDao(daoType.MEMORY).saveObject(boeing787);

		Airport vko = new Airport(
				"Vnukovo International Airport",
				"VKO",
				new Terrain("", "Russian Federation", "Moscow", "", "", 0, 0, 0));
		Airport svo = new Airport(
				"Sheremetyevo International Airport",
				"SVO",
				new Terrain("", "Russian Federation", "Moscow", "", "", 0, 0, 0));
		Airport dme = new Airport(
				"Moscow Domodedovo Airpor",
				"DME",
				new Terrain("", "Russian Federation", "Moscow", "", "", 0, 0, 0));
		Airport wlg = new Airport(
				"Wellington International Airport (Rongotai)", "WLG",
				new Terrain("", "New Zealand", "Wellington", "", "", 0, 0, 0));
		Airport lhr = new Airport("London Heathrow Airport", "LHR",
				new Terrain("",
						"United Kingdom of Great Britain and Northern Ireland",
						"London", "", "", 0, 0, 0));
		Airport jfk = new Airport("John F. Kennedy International Airport",
				"JFK", new Terrain("", "United States of America",
						"New York City", "", "", 0, 0, 0));
		Airport bkk = new Airport("Suvarnabhumi", "BKK", new Terrain("",
				"Bangkok", "Kingdom of Thailand", "", "", 0, 0, 0));
		Airport mia = new Airport("Miami International Airport", "MIA",
				new Terrain("", "United States of America", "Miami", "", "", 0,
						0, 0));
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(mia);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(bkk);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(jfk);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(lhr);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(wlg);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(dme);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(svo);
		server.core.dao.DaoFactory.getDao(daoType.MEMORY).saveObject(vko);

		Airport fue = new Airport("Fuerteventura", "FUE", new Terrain("",
				"Kingdom of Spain", "Puerto del Rosario", "", "", 28.452778f,
				-13.863889f, 0));
		Airport mad = new Airport("Aeropuerto de Madrid-Barajas", "MAD",
				new Terrain("", "Kingdom of Spain", "Madrid", "", "",
						40.484167f, -3.566667f, 0));
		Airport bcn = new Airport("Barcelona El Prat Airport", "BCN",
				new Terrain("", "Kingdom of Spain", "Barcelona", "", "",
						41.296944f, 2.078333f, 0));
		Airport hkg = new Airport("Chek Lap Kok Airport", "HKG", new Terrain(
				"", "People's Republic of China", "Hong Kong", "", "",
				22.308889f, 113.914444f, 0));
		Airport fra = new Airport("Rhein-Main-Flughafen", "FRA", new Terrain(
				"", "Federal Republic of Germany", "Frankfurt am Main", "", "",
				50.033333f, 8.566667f, 0));
		Airport muc = new Airport("Munich Airport", "MUC", new Terrain("",
				"Federal Republic of Germany", "Munich", "", "", 48.353889f,
				11.786111f, 0));
		Airport per = new Airport("Perth Airport", "PER", new Terrain("",
				"Commonwealth of Australia", "Perth", "", "", -31.94335f,
				115.956072f, 8f));
		Airport cck = new Airport("Cocos (Keeling) Islands Airport", "CCK",
				new Terrain("", "Commonwealth of Australia", "West Island", "",
						"", -12.188333f, 96.83f, 6.5f));
		Airport xch = new Airport("Christmas Island Airport", "XCH",
				new Terrain("", "Commonwealth of Australia",
						"Christmas Island", "", "", -10.450556f, 105.690278f,
						7f));
		DaoFactory.getDao(daoType.MEMORY).saveObject(fue);
		DaoFactory.getDao(daoType.MEMORY).saveObject(mad);
		DaoFactory.getDao(daoType.MEMORY).saveObject(bcn);
		DaoFactory.getDao(daoType.MEMORY).saveObject(hkg);
		DaoFactory.getDao(daoType.MEMORY).saveObject(fra);
		DaoFactory.getDao(daoType.MEMORY).saveObject(muc);
		DaoFactory.getDao(daoType.MEMORY).saveObject(per);
		DaoFactory.getDao(daoType.MEMORY).saveObject(cck);
		DaoFactory.getDao(daoType.MEMORY).saveObject(xch);

		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(dme, muc, new FormatDate(17, 5, 2012, 12,00)));
//				, new FormatDate(17, 5, 2012, 13, 20),new FormatDate(0, 0, 0, 3, 20), "Air Berlin"));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(muc, fue, new FormatDate(17, 5, 2012, 14,30)));
//				, new FormatDate(17, 5, 2012, 18, 5),new FormatDate(0, 0, 0, 4, 35), "Air Berlin"));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(fue, bcn, new FormatDate(3, 6, 2012, 18,55)));
//				, new FormatDate(3, 6, 2012, 22, 55),new FormatDate(3, 0), "Vueling Airlines"));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(bcn, dme, new FormatDate(3, 6, 2012, 23,55)));
//				, new FormatDate(4, 6, 2012, 6, 10), new FormatDate(0, 0, 0, 4, 15), "Vueling Airlines"));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(per, xch, new FormatDate(22, 2, 2013, 8, 20)));
//				,new FormatDate(22, 2, 2013, 11, 10), new FormatDate(0,0, 0, 3, 50), "Virgin Australia"));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(xch, cck, new FormatDate(22, 2, 2013, 12, 00)));
//				,new FormatDate(22, 2, 2013, 13, 5), new FormatDate(0,0, 0, 1, 35), "Virgin Australia"));

		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(vko, wlg, new FormatDate(21, 11, 2012, 8,30)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(dme, lhr, new FormatDate(21, 11, 2012, 11, 0)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(svo, bkk, new FormatDate(21, 11, 2012, 21, 55)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(mia, jfk, new FormatDate(22, 11, 2012, 4,52)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(mia, jfk, new FormatDate(23, 11, 2012, 15, 26)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(mia, jfk, new FormatDate(25, 11, 2012)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(dme, muc, new FormatDate(1, 0, 2013, 15, 45)));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new Flight(muc, lhr,new FormatDate(2, 0, 2013, 7, 0)));
		
		for(TicketClass tc:TicketClass.values())
			for(int p=99;p<300;p+=p)
				for(int n=0;n<3;n++){
					Iterator<Flight> itr=(Iterator<Flight>) ((MemoryDAO) DaoFactory.getDao(daoType.MEMORY)).getEntityMap(Flight.class).values().iterator();
					while(itr.hasNext())
						DaoFactory.getDao(daoType.MEMORY).saveObject(
								new Ticket(itr.next(), tc, p));
				}
		
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new User("Jack", "jACK", 127));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new User(" ", " ", 1));
		DaoFactory.getDao(daoType.MEMORY).saveObject(
				new User("Bill", "bILL", 1|2));
		LOCAL_LOG.info("Dao load default");
	}
	
private static void writeDao(){
try {
	((FileDAO) DaoFactory.getDao(daoType.FILE)).writeHashMap(Airplane.class);
	((FileDAO) DaoFactory.getDao(daoType.FILE)).writeHashMap(Airport.class);
	((FileDAO) DaoFactory.getDao(daoType.FILE)).writeHashMap(Flight.class);
	((FileDAO) DaoFactory.getDao(daoType.FILE)).writeHashMap(Terrain.class);
	((FileDAO) DaoFactory.getDao(daoType.FILE)).writeHashMap(Ticket.class);
	((FileDAO) DaoFactory.getDao(daoType.FILE)).writeHashMap(User.class);
	
	new File("./cfg.bin").createNewFile();
	LOCAL_LOG.info("Wrote file");
} catch (IOException e) {
	e.printStackTrace();
}
}

	
	private static void readDao(){
		try {
			((FileDAO) DaoFactory.getDao(daoType.FILE)).readHashMap(Airplane.class);
			((FileDAO) DaoFactory.getDao(daoType.FILE)).readHashMap(Airport.class);
			((FileDAO) DaoFactory.getDao(daoType.FILE)).readHashMap(Flight.class);
			((FileDAO) DaoFactory.getDao(daoType.FILE)).readHashMap(Terrain.class);
			((FileDAO) DaoFactory.getDao(daoType.FILE)).readHashMap(Ticket.class);
			((FileDAO) DaoFactory.getDao(daoType.FILE)).readHashMap(User.class);
			
			LOCAL_LOG.info("Read file");
		} catch (Throwable e) {
			e.getLocalizedMessage();
			defaultDao();
		}
	}

}
